package puan.com.weighBox.adapter;

import android.bluetooth.BluetoothProfile;
import android.content.Context;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.evan.ble.BleManager;
import com.evan.ble.data.BleDevice;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import puan.com.weighBox.R;

public class BluetoothListAdapter extends RecyclerView.Adapter<BluetoothListAdapter.ViewHolder> {

    private Context context;
    private List<BleDevice> list;

    public BluetoothListAdapter(Context context, List<BleDevice> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.bluetooth_list_item, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final BleDevice device = list.get(position);
        holder.tvRssi.setText(device.getRssi() + "");
        if (device.getDevice().getName() != null && !TextUtils.isEmpty(device.getDevice().getName())) {
            holder.tvName.setText(device.getDevice().getName());
        } else {
            holder.tvName.setText("NULL");
        }
        holder.tvSave.setVisibility(View.GONE);
        int state = BleManager.getInstance().getConnectState(device);
        if (BluetoothProfile.STATE_CONNECTED == state) {
            holder.tvOnLine.setVisibility(View.VISIBLE);
            holder.tvOffline.setVisibility(View.GONE);
        } else {
            holder. tvOffline.setVisibility(View.VISIBLE);
            holder. tvOnLine.setVisibility(View.GONE);
        }
        holder.tvMac.setText(device.getDevice().getAddress());

        holder.rootView.setOnClickListener(view -> {
            if(listener!=null){
                listener.onItemClick(device);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public interface OnBluetoothItemClickListener {
        void onItemClick(BleDevice bleDevice);
    }

    private OnBluetoothItemClickListener listener;

    public void setOnBluetoothItemClickListener(OnBluetoothItemClickListener onBluetoothItemClickListener) {
        this.listener = onBluetoothItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvName,tvMac,tvRssi,tvSave,tvOnLine,tvOffline;
        private final RelativeLayout rootView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);
            tvName = itemView.findViewById(R.id.tv_name);
            tvMac = itemView.findViewById(R.id.tv_mac);
            tvRssi = itemView.findViewById(R.id.tv_rssi);
            tvSave = itemView.findViewById(R.id.tv_save);
            tvOnLine = itemView.findViewById(R.id.tv_online);
            tvOffline = itemView.findViewById(R.id.tv_offline);
        }
    }
}
